package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 聚合函数,查看1年级1班有多少人
 */
public class JDBCDemo7 {
    public static void main(String[] args) {
        /**
          select count(*) count
          from class c
          join student s on c.id=s.class_id
          where c.name='1年级1班'
         */
        try(Connection connection=DBUtil.getConnection();){
            Statement statement=connection.createStatement();
            String sql=" select count(*) count " +
                    "          from class c " +
                    "          join student s on c.id=s.class_id " +
                    "          where c.name='1年级1班'";
            ResultSet rs=statement.executeQuery(sql);
            while (rs.next()){
                //因为本次查询只有一条数据,所以也可以用if
                int count=rs.getInt("count");
                System.out.println("1年级1班有"+count+"人");
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
